package com.qianyu.project.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.project.core.context.user.FlowUserManager;
import com.qianyu.project.entity.*;
import com.qianyu.project.mapper.SubBiddingHistoryMapper;
import com.qianyu.project.service.ISubBiddingHistroyService;
import com.qianyu.project.service.ISubTenderPlanService;
import com.qianyu.project.service.ISystemUserService;
import com.qianyu.project.service.ITenderCompanyService;
import com.qianyu.project.utils.AssertTender;
import com.qianyu.project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author gs
 * @date create in 2021/2/3 10:29
 */
@Service
public class SubBiddingHistroyServiceImpl extends ServiceImpl<SubBiddingHistoryMapper, SubBiddingHistory> implements ISubBiddingHistroyService {

    @Autowired
    private SubBiddingHistoryMapper subBiddingHistoryMapper;
    @Autowired
    private FlowUserManager flowUserManager;
    @Autowired
    private ISystemUserService systemUserService;
    @Autowired
    private ITenderCompanyService tenderCompanyService;
    @Autowired
    private ISubTenderPlanService subTenderPlanService;

    @Override
    public void submit(SubBiddingHistory subBiddingHistory) {
        Integer companyId = AssertTender.isValidate(systemUserService, flowUserManager, tenderCompanyService, subBiddingHistory.getOffer().doubleValue());
        //设置默认值
        subBiddingHistory.setCreatedate(DateUtil.now());
        subBiddingHistory.setCompanyId(companyId);
        subBiddingHistoryMapper.insert(subBiddingHistory);
    }

    @Override
    public SubBiddingHistory isExist(String subId) {
        SystemUser systemUser = systemUserService.getById(flowUserManager.getFlowUser().getId());
        return subBiddingHistoryMapper.selectOne(
                new QueryWrapper<SubBiddingHistory>()
                .eq("company_id", systemUser.getCompanyId())
                .eq("sub_id", subId)
                .ne("status", "5")
        );
    }

    @Override
    public void update(SubBiddingHistory subBiddingHistory) {
        //查询公司注册资本
        AssertTender.isValidate(systemUserService, flowUserManager, tenderCompanyService, subBiddingHistory.getOffer().doubleValue());
        subBiddingHistory.setCreatedate(DateUtil.now());
        subBiddingHistoryMapper.updateById(subBiddingHistory);
    }

    @Override
    public IPage<SubBiddingHistory> getPageList(Map<String, String> params) {
        SystemUser systemUser = systemUserService.getById(flowUserManager.getFlowUser().getId());
        //验证公司是否为空
        if(systemUser.getCompanyId() != null){
            params.put("companyId", systemUser.getCompanyId().toString());
        }
        return subBiddingHistoryMapper.selectPageList(PageUtil.createPage(params),params);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void cooperation(SubBiddingHistory subBiddingHistory) {
        //修改当前记录为中标
        subBiddingHistoryMapper.updateById(subBiddingHistory);
        //修改其他竞争对手为未中标
        SubBiddingHistory other = new SubBiddingHistory();
        other.setStatus("4");
        subBiddingHistoryMapper.update(
                other,
                new UpdateWrapper<SubBiddingHistory>()
                        .eq("sub_id", subBiddingHistory.getSubId())
                        .ne("id", subBiddingHistory.getId())
        );
        //修改招标对象状态为中标
        SubTenderPlan subTenderPlan = new SubTenderPlan();
        subTenderPlan.setId(subBiddingHistory.getSubId());
        subTenderPlan.setStatus("3");
        subTenderPlanService.updateById(subTenderPlan);
    }
}
